const app = getApp()
var queryUrl = app.globalData.queryUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    videoDetail: '',
    start: 0,
    current: 0,
    startTouch: '',
    startNum: '0',
    //其中的 视频url(videoUrl) 封面url(videoImageUrl) 头像url(headUrl) 的话大家自己填上就行
    videoList: [], //接口返回的视频列表。
    touch: false,
    touchStartTime: 0, //触摸开始时间
    touchEndTime: 0, // 触摸结束时间
    lastTapTime: 0, // 单击事件点击后要触发的函数
    lastTapTimeoutFunc: null,
    switchTo: true,
    page: 1,
    percent: 0,
    autoplay: true,
    play: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.videoContext = wx.createVideoContext('videoplayer');
    this.setData({
      updateState: true
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    // var listTem = that.data.videoList;
    // for (var i = 0; i < listTem.length; i++) {
    //   var arr = (listTem[i].quickProjectWorkerTagName).split(',')
    //   // console.log(arr)
    //   listTem[i].tagnamesArr = arr;
    // }
    // that.setData({
    //   videoList: listTem
    // })
    that.setData({ page: 1 })
    that.getVideoList(that.data.page)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  // 下面主要模仿滑动事件
  touchstart: function (e) {
    console.log(e);
    this.setData({
      // touchStartTime: e.timeStamp,
      showGuide: false
    })
    let startTouch = e.changedTouches[0]
    this.setData({
      startTouch: startTouch,
      touch: false
    })
  },
  touchmove: function (e) {
    let Y = e.changedTouches[0].pageY - this.data.startTouch.pageY;
  },
  touchend: function (e) {
    // this.setData({
    //   touchEndTime: e.timeStamp
    // })
    var that = this;

    if (Math.abs(e.changedTouches[0] - e.changedTouches[0]) < 40) {
      this.videoContext = wx.createVideoContext('videoplayer')
      if (!this.data.play) {
        //暂停播放
        this.videoContext.pause()//暂停播放
        this.setData({
          play: true
        })
      } else {
        //当play==false 显示图片 开始播放
        this.videoContext.play()//开始播放
        this.setData({
          play: false
        })
      }
    }
    console.log(this.data.startTouch + '----' + e.changedTouches[0]);
    this.getDirect(this.data.startTouch, e.changedTouches[0])
  },
  touchcancel: function (e) {
    this.getDirect(this.data.startTouch, e.changedTouches[0])
  },
  // 计算滑动方向
  getDirect: function (start, end) {
    console.log(start.pageY + '----' + end.pageY);
    var X = end.pageX - start.pageX,
      Y = end.pageY - start.pageY;

    if (Math.abs(X) > Math.abs(Y) && X > 0) {
      console.log("left 2 right");
    }
    else if (Math.abs(X) > Math.abs(Y) && X < 0) {
      console.log("right 2 left");
    }
    else if (Math.abs(Y) > Math.abs(X) && Y > 40) {
      if (this.data.current > 0) {
        this.setData({
          touch: true,
          transitionOver: false
        })
        this.pre()
      } else {
        this.setData({
          current: 0
        })
      }
    }
    else if (Math.abs(Y) > Math.abs(X) && Y < -40) {
      if (this.data.current < this.data.videoList.length - 2) {
        this.setData({
          touch: true
        })
        this.next()
      } else {
        var startNum = parseInt(this.data.startNum) + 5;
        this.setData({
          startNum: startNum,
        })
        this.getVideoList(this.data.page);
        this.setData({
          current: this.data.videoList.length - 1
        })
      }
    }
  },
  // 播放上一个
  pre: function () {
    this.setData({
      current: this.data.current - 1,
      play: false
    })
    console.log(this.data.current)
  },

  // 播放下一个
  next: function () {
    var that = this;
    console.log('current:' + that.data.current + '-----videolist-length:' + that.data.videoList.length)
    if (that.data.current == that.data.videoList.length) {
      wx.showToast({
        title: '没有更多视频了！',
        icon: 'none'
      })
      return false
    }

    this.setData({
      current: this.data.current + 1,
      play: false
    })
    console.log(that.data.current)
  },
  //点击暂停/开始
  videoTap: function () {
    //获取video
    this.videoContext = wx.createVideoContext('videoplayer')
    if (this.data.play) {
      //开始播放
      this.videoContext.play()//开始播放
      this.setData({
        play: false
      })
    } else {
      //当play==false 显示图片 暂停
      this.videoContext.pause()//暂停播放
      this.setData({
        play: true
      })
    }
  },
  //播放条时间改表触发
  videoUpdate(e) {
    // if (this.data.updateState) { //判断拖拽完成后才触发更新，避免拖拽失效
    //   let sliderValue = e.detail.currentTime / e.detail.duration * 100;
    //   this.setData({
    //     sliderValue: sliderValue,
    //     duration: e.detail.duration
    //   })
    // }
    var val = e.detail.currentTime;
    var max = e.detail.duration;
    var percent = Math.round(val / max * 10000) / 100;
    this.setData({ percent: percent })
  },
  sliderChanging(e) {
    this.setData({
      updateState: false //拖拽过程中，不允许更新进度条
    })
  },
  //拖动进度条触发事件
  sliderChange(e) {
    if (this.data.duration) {
      this.videoContext.seek(e.detail.value / 100 * this.data.duration);
      //完成拖动后，计算对应时间并跳转到指定位置
      this.setData({
        sliderValue: e.detail.value,
        updateState: true //完成拖动后允许更新滚动条
      })
    }
  },
  getVideoList: function (page) {
    console.log('page:----' + this.data.page)
    var that = this;
    var totalNum = this.data.totalNum;
    wx.request({
      url: queryUrl + 'index/index',
      data: { page: that.data.page },
      success: function (res) {
        console.log(res);
        var videoList = res.data;
        var videoList = that.data.videoList.concat(videoList)
        totalNum = videoList.length;
        that.setData({
          videoList: videoList,
          totalNum: totalNum,
          page: that.data.page + 1
        })
      }
    })
  },
})